﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using StringDic = System.Collections.Generic.Dictionary<string, string>;

namespace CommonLib.Reflexion.Core.IO.Util
{
    public class XmlUtil
    {
        private XmlDocument doc = new XmlDocument();

        #region ctors
        public XmlUtil(XmlDocument doc)
        {
            this.doc = doc;
        }
        public XmlUtil(string xmlstring)
        {
            doc.LoadXml(xmlstring);
        }
        public XmlUtil(Stream xmlstream)
        {
            doc.Load(xmlstream);
        }
        #endregion

        #region Get Node Methods
        public XmlNode GetSingleNode(string element)
        {
            return doc.DocumentElement.SelectSingleNode(element);
        }
        public XmlNode GetSigleNode(XmlNode node, string element)
        {
            return node.SelectSingleNode(element);
        }
        public XmlNode[] GetMultipleNode(string element)
        {
            List<XmlNode> retList = new List<XmlNode>();
            XmlNodeList list = doc.DocumentElement.SelectNodes(element);

            for (int i = 0; i < list.Count; i++)
                retList.Add(list[i]);

            return retList.ToArray();
        }
        #endregion

        #region Get SingleNode Value
        public string GetSingleNodeValue(string element)
        {
            return GetSingleNode(element).InnerText;
        }
        public string GetSingleNodeValue(XmlNode node, string element)
        {
            return node.SelectSingleNode(element).InnerText;
        }
        public StringDic GetSingleNodeValues(XmlNode node, params string[] elements)
        {
            StringDic dic = new StringDic();

            foreach (string item in elements)
                dic.Add(item, GetSingleNodeValue(node, item));

            return dic;
        }
        public StringDic GetSingleNodeValues(params string[] elements)
        {
            StringDic dic = new StringDic();

            foreach (string item in elements)
                dic.Add(item, GetSingleNodeValue(item));

            return dic;
        }
        #endregion

        #region Get Attribute Value
        public string GetAttributeValue(string parentNode, string attribute)
        {
            XmlNode node = GetSingleNode(parentNode);
            return node.Attributes[attribute].InnerText;
        }
        public StringDic GetAttributeValues(string parentNode, params string[] attributes)
        {
            StringDic retItems = new StringDic();
            XmlNode node = GetSingleNode(parentNode);

            foreach (string attr in attributes)
                retItems.Add(attr, node.Attributes[attr].InnerText);

            return retItems;
        }
        public StringDic GetAllAttributesValues(string parentNode)
        {
            StringDic dic = new StringDic();
            XmlNode node = GetSingleNode(parentNode);

            foreach (XmlAttribute attr in node.Attributes)
                dic.Add(attr.Name, attr.InnerText);

            return dic;
        }
        #endregion

        #region Get SingleNode Value With Attribute Value
        public StringDic GetNodeValueWithAttr(XmlNode node)
        {
            StringDic dic = new StringDic();
            dic.Add(node.Name, node.InnerText);

            foreach (XmlAttribute attr in node.Attributes)
                dic.Add(attr.Name, attr.InnerText);

            return dic;
        }
        public StringDic GetNodeValueWithAttr(string parentNode)
        {
            StringDic dic = new StringDic();

            XmlNode node = GetSingleNode(parentNode);
            dic.Add(node.Name, node.InnerText);

            foreach (XmlAttribute attr in node.Attributes)
                dic.Add(attr.Name, attr.InnerText);

            return dic;
        }
        #endregion
    }
}
